package com.polaris.live.dialog

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.view.View
import androidx.viewpager2.widget.ViewPager2
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.adapter.level.NewGuideBannerAdapter
import com.polaris.live.cache.CommonCache
import com.polaris.live.common.ext.dp
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.databinding.DialogNewLivingGuideBinding
import com.polaris.live.resp.event.NewLivingTimeEvent
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router
import com.zhpan.indicator.enums.IndicatorSlideMode
import org.greenrobot.eventbus.EventBus

/**
 * NewLivingGuideDialog
 *
 * @author Created by 半仙 on 2024/1/18/018 10:53
 */
class NewLivingGuideDialog(
    context: Context,
    private val onDestroyCallback: ((isClick: Boolean) -> Unit)? = null,
) : CenterPopupView(context) {

    private lateinit var mBinding: DialogNewLivingGuideBinding

    private val mAdapter by lazy { NewGuideBannerAdapter() }

    //有没有点过按钮  如果点过则为true   如果为true外部就不需要继续执行上一步的操作  如果为false则继续执行外界的上一步操作
    private var mIsClick = false

    override fun getImplLayoutId() = R.layout.dialog_new_living_guide

    override fun onCreate() {
        super.onCreate()
        mBinding = DialogNewLivingGuideBinding.bind(popupImplView)

        CommonCache.setNewLivingTime()
        val count = CommonCache.getNewLivingCount() + 1
        CommonCache.setNewLivingCount(count)

        mBinding.levelBanner.apply {
            registerLifecycleObserver(lifecycle)
            setAdapter(mAdapter)
            setIndicatorSlideMode(IndicatorSlideMode.NORMAL)
            setIndicatorVisibility(View.GONE)
            setRoundCorner(12.dp)
            setAutoPlay(false)
            setCanLoop(false)
            registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    when (position) {
                        0 -> {
                            mBinding.ivLeft.visibility = View.GONE
                            mBinding.ivRight.visibility = View.VISIBLE
                        }

                        1 -> {
                            mBinding.ivLeft.visibility = View.VISIBLE
                            mBinding.ivRight.visibility = View.VISIBLE
                        }

                        2 -> {
                            mBinding.ivLeft.visibility = View.VISIBLE
                            mBinding.ivRight.visibility = View.GONE
                        }
                    }
                }
            })
        }
        mBinding.ivLeft.setOnSingleClickListener {
            val getNow = mBinding.levelBanner.currentItem
            if (getNow - 1 >= 0) {
                mBinding.levelBanner.setCurrentItem(getNow - 1, true)
            }
        }
        mBinding.ivRight.setOnSingleClickListener {
            val getNow = mBinding.levelBanner.currentItem
            if (getNow + 1 <= mAdapter.itemCount) {
                mBinding.levelBanner.setCurrentItem(getNow + 1, true)
            }
        }
        mBinding.levelBanner.create(listOf("", "", ""))
        mBinding.ivClose.setOnSingleClickListener {
            dismiss()
        }
        mAdapter.setOnCancelClick {
            mIsClick = true
            if (it != 0) {
                EventBus.getDefault().post(NewLivingTimeEvent(true))
            }
            dismiss()
        }

        mAdapter.setOnStartClick {
            mIsClick = true
            when (it) {
                0 -> EventBus.getDefault().post(NewLivingTimeEvent(true))

                1 -> Router.builder(RoutePath.MY_ANCHOR_LEVEL_ACTIVITY).navigation(context)

                2 -> Router.builder(RoutePath.MYBEANS_ACTIVITY).navigation(context)
            }
            dismiss()
        }
        animationStart()
        animationStartRight()

    }

    private fun animationStart() {
        val translateAnimationLeft =
            ObjectAnimator.ofFloat(mBinding.ivLeft, "translationX", 0f, -40f)
        translateAnimationLeft.duration = 500

        val translateAnimationRight =
            ObjectAnimator.ofFloat(mBinding.ivLeft, "translationX", -40f, 0f)
        translateAnimationRight.duration = 500

        val animatorSet = AnimatorSet()
        animatorSet.playSequentially(translateAnimationLeft, translateAnimationRight)

        animatorSet.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                super.onAnimationEnd(animation)
                if (!isDismiss) {
                    animatorSet.start()
                }
            }
        })

        animatorSet.start()
    }

    private fun animationStartRight() {
        val translateAnimationLeft =
            ObjectAnimator.ofFloat(mBinding.ivRight, "translationX", 40f, 0f)
        translateAnimationLeft.duration = 500

        val translateAnimationRight =
            ObjectAnimator.ofFloat(mBinding.ivRight, "translationX", 0f, 40f)
        translateAnimationRight.duration = 500

        val animatorSet = AnimatorSet()
        animatorSet.playSequentially(translateAnimationRight, translateAnimationLeft)

        animatorSet.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                super.onAnimationEnd(animation)
                if (!isDismiss) {
                    animatorSet.start()
                }
            }
        })

        animatorSet.start()
    }

    override fun onDismiss() {
        onDestroyCallback?.invoke(mIsClick)
        super.onDismiss()
        //通知mainActivity开始计时
        EventBus.getDefault().post(NewLivingTimeEvent())
    }
}